/*
 *-------------------------------------------------------------------
 *
 * LIST.C
 *
 * Code for manipulating the cells linked list
 * This code is NOT totally implementation-independent
 * the routines are written for the kind of data
 * they are manipulating
 * 
 * Marcelo Walter
 *
 *-------------------------------------------------------------------
 */

/*
 *-------------------------------------------------------------------
 *   INCLUDES
 *-------------------------------------------------------------------
 */
#include <stdio.h>
#include <stdlib.h>
#include "common.h"

/*
 *---------------------------------------------------------------
 *
 * GLOBAL VARIABLES
 *
 *---------------------------------------------------------------
 */
/* head and tail nodes of the global linked list */
CELL *headC;
CELL *zedC;
int cell_size = sizeof(CELL);

/*
 *-----------------------------------------------------------
 * Free the space used by the global linked list of cells
 *-----------------------------------------------------------
 */
void free_cells_list(void)
{
  CELL *s, *to_remove;

  s=headC->next;
  while(s != zedC){
    to_remove = s;
    s = to_remove->next;
    remove_list(to_remove,headC);
  }
}

/* 
 *-----------------------------------------------------------
 * Creates the list. Initialize head and
 * zed (dummy node). Head and zed are global
 * variables
 *---------------------------------------------------------------
 */
void create_global_list(void)
{
  headC=palloc();
  zedC=palloc();
  
  headC->next = zedC;
  zedC->next = (CELL *)NULL;
}

/*
 *-----------------------------------------------------------
 * Allocate space for a new node and returns
 * a pointer to the newly allocated node.
 * If malloc cannot find space the routine
 * exits.
 *-----------------------------------------------------------
*/
CELL *palloc(void)
{
  CELL *new;

  if((new = (CELL *)malloc(cell_size)) == NULL)
      errorMsg("Error with malloc in function palloc()!");
  
  new->next = (CELL *)NULL;
  return new;
}

/*
 *-----------------------------------------------------------
 *  Inserts a node in the list
 *  Receives a pointer to the node to be
 *  inserted, a pointer to the head
 *  node of the list and to the
 *  tail node
 *-----------------------------------------------------------
 */
void insert(CELL *p, CELL *h, CELL *t)
{
  CELL *s;

  s = h;
  while(s->next != t) s = s->next;
  
  s->next = p;
  p->next = t;
}	

/*
 *-----------------------------------------------------------
 *  Prints the list. Receives a pointer
 *  to the head and tail nodes of
 *  the list
 *-----------------------------------------------------------
 */
void print_list(CELL *h, CELL *t)
{
  CELL *s;
  
  printf("Printing Global List:\n");
  s = h->next;
  while (s != t){
    printf("x = %3.2f y = %3.2f type = %d split = %d gen = %d\n",
	   s->x, s->y, s->ctype, s->tsplit, s->gen);
    s = s->next;
  }
}


/*
 *-----------------------------------------------------------
 *  Removes a node from the list.
 *  Receives a pointer to the head of the
 *  list and to the node to be removed
 *-----------------------------------------------------------
 */
void remove_list(CELL *p, CELL *h)
{
  CELL *s;
  
  s = h;
  while(s->next != p) s = s->next;
  
  s->next = p->next;
  free(p);
}






















